package com.ruanyun.modular.ry.api.web;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.HttpContext;
import com.ruanyun.api.core.constant.JwtConstants;
import com.ruanyun.api.core.util.JwtTokenUtil;
import com.ruanyun.base.shiro.ShiroUser;
import com.ruanyun.sys.core.shiro.ShiroKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class RyController extends BaseController {

    /**
     * 获取用户Id
     *
     * @return
     */

    protected Long getCustomerId() {
        //获取当前登录用户
        String str = getHttpServletRequest().getHeader(JwtConstants.AUTH_HEADER);
        if (StrUtil.isBlank(str)) {
            return 0L;
        }
        String authToken = str.substring(7);
        if (StrUtil.isBlank(authToken)) {
            return 0L;
        }
        try {
            String userId = JwtTokenUtil.getUsernameFromToken(authToken);
            return Long.valueOf(userId);
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0L;
        }
    }


    /**
     * 判断是否登陆
     *
     * @return
     */
    protected Boolean isLogin() {
        String token = HttpContext.getRequest().getHeader(JwtConstants.SECRET);
        if (token == null) {
            return false;
        }
        Boolean expired = JwtTokenUtil.isTokenExpired(token);
        if (expired) {
            return false;
        }
        return true;
    }

    /**
     * @return
     */
    protected Long getSysUserId() {
        ShiroUser userNotNull = ShiroKit.getUserNotNull();
        return Long.valueOf(userNotNull.getId());
    }

    /**
     * @return
     */
    protected ShiroUser getShiroUser() {
        ShiroUser userNotNull = ShiroKit.getUserNotNull();
        return userNotNull;
    }
}
